var ns, app, WXPhotoPicker;
app = new Vue({
    el: '#app',
    data: {
        form: {
            VisitorName: '',
            PhoneNumber: '',
            IDCardNumber: '',
            VisitCount: '',
            ItemName: '',
            CarNumber: '',
            ProjectID: 0,
            ProjectName: '',
            VisitedName: '',
            VisitedPhone: '',
            VisitTypeID: 0,
            ReleaseExplain: '',
        },
        itemform: {
            ProjectID: 0,
            ProjectName: '',
            VisitorName: '',
            PhoneNumber: '',
            VisitedName: '',
            IDCardNumber: '',
            ItemName: '',
            CarNumber: '',
            VisitTypeID: 0,
            ReleaseExplain: '',//放行说明
        },
        status: 1,
        typelist1: [],
        typelist2: [],
        //上传图片
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        txtVal: 0,

    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "getvisittypelist";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.typelist1 = data.list1;
                    that.typelist2 = data.list2;
                    if (that.typelist1.length > 0 && that.status == 1) {
                        that.do_select_type(that.typelist1[0]);
                    }
                    if (that.typelist2.length > 0 && that.status == 2) {
                        that.do_select_type(that.typelist2[0]);
                    }

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        select_status: function(status) {
            var that = this;
            that.status = status;
            if (that.typelist1.length > 0 && that.status == 1) {
                that.do_select_type(that.typelist1[0]);
            }
            if (that.typelist2.length > 0 && that.status == 2) {
                that.do_select_type(that.typelist2[0]);
            }
        },
        do_select_type: function (item) {
            var that = this;
            var list = that.typelist1;
            if (that.status == 1) {
                that.form.VisitTypeID = item.ID;
            }        
            if (that.status == 2) {
                list = that.typelist2;
                that.itemform.VisitTypeID = item.ID;
            }

            for (var i = 0; i < list.length; i++) {
                if (item.ID == list[i].ID) {
                    continue;
                }
                list[i].isselect = false;
            }
            item.isselect = true;

        },
        do_save: function() {
            var that = this;
            var options = {};
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            if (that.status == 1) {
                if (that.form.VisitorName == '') {
                    api.toast({
                        msg: '请填写来访人姓名',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.form.VisitTypeID == 0 || that.form.VisitTypeID == undefined) {
                    api.toast({
                        msg: '请选择来访事由',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.form.ProjectID <= 0) {
                    api.toast({
                        msg: '请选择被访人楼号',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.form.PhoneNumber != '' && !that.checknumber(that.form.PhoneNumber)) {
                    api.toast({
                        msg: '手机号码输入不正确',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.form.IDCardNumber != '' && !that.checknumber(that.form.IDCardNumber)) {
                    api.toast({
                        msg: '身份证输入不正确',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
               
                options = ns.extend(that.form, {
                    action: 'dosavevisit',
                    type: 1
                });
            } else {
                if (that.itemform.ProjectID <= 0) {
                    api.toast({
                        msg: '请选择业主楼号',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.itemform.VisitTypeID == 0 || that.itemform.VisitTypeID == undefined) {
                    api.toast({
                        msg: '请选择放行事由',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.itemform.PhoneNumber != '' && !that.checknumber(that.itemform.PhoneNumber)) {
                    api.toast({
                        msg: '联系方式输入不正确',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.itemform.IDCardNumber != '' && !that.checknumber(that.itemform.IDCardNumber)) {
                    api.toast({
                        msg: '证件号码输入不正确',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                options = ns.extend(that.itemform, {
                    action: 'dosavevisit',
                    type: 2
                });
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    // 保存成功后是否跳转到门岗出入列表
                    setTimeout(function() {
                        api.closeWin();
                    }, 500)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        open_history: function() {
            ns.openWin('gate_sentry_history_frm', '门岗出入记录');
        },
        do_select_project: function() {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目', {
                selecttype: 1,
            });
        },
        get_select_project: function(data) {
            var that = this;
            if (that.status == 1) {
                if (data.roomid > 0) {
                    that.form.ProjectID = data.roomid;
                } else if (data.projectid > 0) {
                    that.form.ProjectID = data.projectid;
                }
                that.form.ProjectName = data.name;
            } else {
                if (data.roomid > 0) {
                    that.itemform.ProjectID = data.roomid;
                } else if (data.projectid > 0) {
                    that.itemform.ProjectID = data.projectid;
                }
                that.itemform.ProjectName = data.name;
            }
            that.get_relation_name();
        },
        checknumber: function(str) {
            var re = /^[0-9a-zA-Z]*$/g; //判断字符串是否为数字和字母组合
            if (!re.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        get_relation_name: function() {
            var that = this;
            var options = {};
            options.action = 'getroomrelationdata';
            if (that.status == 1) {
                options.ProjectID = that.form.ProjectID;

            } else {
                options.ProjectID = that.itemform.ProjectID;
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if (that.status == 1) {
                        that.form.VisitedName = data.RealName;
                        that.form.VisitedPhone = data.PhoneNumber;
                    } else {
                        that.itemform.VisitorName = data.RealName;
                        that.itemform.PhoneNumber = data.PhoneNumber;
                    }
                }
            });
        },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        checkStrLengths: function () {
            var that = this;
            if (that.itemform.ReleaseExplain.length <= 50) {
                that.txtVal = that.itemform.ReleaseExplain.length;
            }
            else {
                that.txtVal = 50;
                that.itemform.ReleaseExplain = that.itemform.ReleaseExplain.substring(0,50);
            }
           
        },

    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    WXPhotoPicker = api.require('WXPhotoPicker');
    setTimeout(function() {
        app.get_data();
    }, 500)
    api.addEventListener({
        name: 'do_open_gatehistory'
    }, function(ret, err) {
        app.open_history();
    });
    app.get_select_project(api.pageParam);
    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function(ret) {
        app.get_select_project(ret.value);
    })
};
